/**
 * Copyright (c) 2011-2016, James Zhan 詹波 (jfinal@126.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.billionsfinance.hessian.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * Prop. Prop can load properties file from CLASSPATH or File object.
 */
public class PropUtils {

	private Properties properties = null;

	private static final String DEFAULT_ENCODING = "UTF-8";

	/**
	 * Prop constructor.
	 * 
	 * @param fileName
	 * @see #Prop(String, String)
	 */
	public PropUtils(String fileName) {
		this(fileName, DEFAULT_ENCODING);
	}

	/**
	 * Prop constructor
	 * <p>
	 * Example:<br>
	 * Prop prop = new Prop("my_config.txt", "UTF-8");<br>
	 * String userName = prop.get("userName");<br>
	 * <br>
	 * 
	 * prop = new Prop("com/jfinal/file_in_sub_path_of_classpath.txt", "UTF-8");
	 * <br>
	 * String value = prop.get("key");
	 * 
	 * @param fileName
	 *            the properties file's name in classpath or the sub directory
	 *            of classpath
	 * @param encoding
	 *            the encoding
	 */
	public PropUtils(String fileName, String encoding) {
		InputStream inputStream = null;
		try {
			inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName); // properties.load(Prop.class.getResourceAsStream(fileName));
			if (inputStream == null) {
				throw new IllegalArgumentException("Properties file not found in classpath: " + fileName);
			}
			properties = new Properties();
			properties.load(new InputStreamReader(inputStream, encoding));
		} catch (IOException e) {
			throw new RuntimeException("Error loading properties file.", e);
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * Prop constructor.
	 * 
	 * @param file
	 * @see #Prop(File, String)
	 */
	public PropUtils(File file) {
		this(file, DEFAULT_ENCODING);
	}

	/**
	 * Prop constructor
	 * <p>
	 * Example:<br>
	 * Prop prop = new Prop(new File("/var/config/my_config.txt"), "UTF-8");<br>
	 * String userName = prop.get("userName");
	 * 
	 * @param file
	 *            the properties File object
	 * @param encoding
	 *            the encoding
	 */
	public PropUtils(File file, String encoding) {
		if (file == null) {
			throw new IllegalArgumentException("File can not be null.");
		}
		if (!file.isFile()) {
			throw new IllegalArgumentException("File not found : " + file.getName());
		}

		InputStream inputStream = null;
		try {
			inputStream = new FileInputStream(file);
			properties = new Properties();
			properties.load(new InputStreamReader(inputStream, encoding));
		} catch (IOException e) {
			throw new RuntimeException("Error loading properties file.", e);
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * @date: 2016年6月30日 下午2:56:10
	 * @Title: get
	 * @param key
	 * @return String 返回值
	 */
	public String get(String key) {
		return properties.getProperty(key);
	}

	/**
	 * @Title: get
	 * @param key
	 * @param defaultValue
	 * @return String 返回值
	 */
	public String get(String key, String defaultValue) {
		return properties.getProperty(key, defaultValue);
	}

	/**
	 * @date: 2016年6月30日 下午2:56:41
	 * @Title: getInt
	 * @param key
	 * @return Integer 返回值
	 */
	public Integer getInt(String key) {
		return getInt(key, null);
	}

	/**
	 * @Title: getInt
	 * @param key
	 * @param defaultValue
	 * @return Integer 返回值
	 */
	public Integer getInt(String key, Integer defaultValue) {
		String value = properties.getProperty(key);
		if (value != null) {
			return Integer.parseInt(value.trim());
		}
		return defaultValue;
	}

	/**
	 * @Title: getLong
	 * @param key
	 * @return Long 返回值
	 */
	public Long getLong(String key) {
		return getLong(key, null);
	}

	/**
	 * @Title: getLong
	 * @param key
	 * @param defaultValue
	 * @return Long 返回值
	 */
	public Long getLong(String key, Long defaultValue) {
		String value = properties.getProperty(key);
		if (value != null) {
			return Long.parseLong(value.trim());
		}
		return defaultValue;
	}

	/**
	 * @date: 2016年6月30日 下午2:57:30
	 * @Title: getBoolean
	 * @param key
	 * @return Boolean 返回值
	 */
	public Boolean getBoolean(String key) {
		return getBoolean(key, null);
	}

	/**
	 * @date: 2016年6月30日 下午2:57:39
	 * @Title: getBoolean
	 * @param key
	 * @param defaultValue
	 * @return Boolean 返回值
	 */
	public Boolean getBoolean(String key, Boolean defaultValue) {
		String value = properties.getProperty(key);
		if (value != null) {
			value = value.toLowerCase().trim();
			if ("true".equals(value)) {
				return true;
			} else if ("false".equals(value)) {
				return false;
			}
			throw new RuntimeException("The value can not parse to Boolean : " + value);
		}
		return defaultValue;
	}

	/**
	 * @date: 2016年6月30日 下午2:57:50
	 * @Title: containsKey
	 * @param key
	 * @return boolean 返回值
	 */
	public boolean containsKey(String key) {
		return properties.containsKey(key);
	}

	public Properties getProperties() {
		return properties;
	}
}
